unit Tasks;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RepItems, StdCtrls, DB, DBClient, ExtCtrls, SQLHelpers;

type
  TTasksForm = class(TForm)
    gbTasks: TGroupBox;
    Timer1: TTimer;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    GroupBox2: TGroupBox;
    Memo2: TMemo;
    Splitter1: TSplitter;
    Panel1: TPanel;
    ScrollBox1: TScrollBox;
    Panel2: TPanel;
    Label1: TLabel;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    procedure ShowThread(ThreadForm: TRepItemsForm);
    procedure AddLog(ItemName, LogText: string);
    procedure CheckClose;
    procedure StartSchedule;
  end;

var
  TasksForm: TTasksForm;

implementation
uses Main;
{$R *.dfm}

{ TScheduleForm }

procedure TTasksForm.AddLog(ItemName, LogText: string);
begin
  //Memo2.Lines.Add(ItemName + ':' + LogText);
  MainForm.StatusBar1.Panels[0].Text := ItemName + ':' + LogText;
  MainForm.StatusBar1.Update;
end;

procedure TTasksForm.CheckClose;
begin
  if ControlCount = 2 then
    Close;
end;

procedure TTasksForm.ShowThread(ThreadForm: TRepItemsForm);
begin
  ThreadForm.Parent := Panel2;
  ThreadForm.Align := alTop;
  ThreadForm.Top := 10000;
  ThreadForm.Show;

//  Show;
end;

procedure TTasksForm.StartSchedule;
begin
//  DBHelper.BackupActiveDBIndex(ScriptDBIndex);
  try
    DBHelper.ExecuteQuery(ClientDataSet1, 'select * from tSchedule', nil, []);
    Timer1.Enabled := True;
  finally
//    DBHelper.RestoreActiveDBIndex;
  end;
end;

procedure TTasksForm.Timer1Timer(Sender: TObject);
begin
  if ClientDataSet1.Locate('ScheduleTime', Time, []) then
    MainForm.GetThread(ClientDataSet1.FieldByName('ThreadID').AsInteger).
      ExecuteSchedule(ClientDataSet1.FieldByName('ScheduleID').AsInteger);
end;

end.
